package pers.youqu.item.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pers.youqu.common.dto.PageDTO;
import pers.youqu.item.dto.SkuDTO;
import pers.youqu.item.dto.SpecParamDTO;
import pers.youqu.item.dto.SpuDTO;
import pers.youqu.item.dto.SpuDetailDTO;
import pers.youqu.item.service.SkuService;
import pers.youqu.item.service.SpuDetailService;
import pers.youqu.item.service.SpuService;

import java.util.List;
import java.util.Map;

/**
 * @author Jason
 * @version 1.0.0
 * @date 2020/12/7 16:12
 * @Description TODO
 */
@RestController
public class GoodsController {

    @Autowired
    private SpuService spuService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuDetailService spuDetailService;

    /**
     * 根据spuId查询spu的所有规格参数值
     * @param spuId 商品规格信息id
     * @param searching 是否搜索
     * @return 参数信息和值
     */
    @GetMapping("/goods/spec/value")
    public ResponseEntity<List<SpecParamDTO>> findSpuAllParamBySpuId(
            @RequestParam("id") Long spuId,
            @RequestParam(value = "searching", required = false) Boolean searching
    ) {
        return ResponseEntity.ok(spuService.findSpuAllParamBySpuId(spuId, searching));
    }

    /**
     * 分页查询spu
     * @param brandId 品牌id
     * @param categoryId 分类id
     * @param spuId spuID
     * @param page 当前页
     * @param rows 每页条数
     * @param saleable 是否上架
     * @return
     */
    @GetMapping("/goods/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> findSpuByPage(
            @RequestParam(value = "brandId",required = false) Long brandId,
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "id",required = false) Long spuId,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable",required = false) Boolean saleable
    ) {
        return ResponseEntity.ok(spuService.findSpuByPage(brandId, categoryId, spuId, page, rows, saleable));
    }

    /**
     * 根据id批量查询sku
     * @param ids skuid集合
     * @return sku信息
     */
    @GetMapping("/goods/sku/list")
    public ResponseEntity<List<SkuDTO>> findSkuBySkuIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(skuService.findSkuBySkuIds(ids));
    }

    /**
     * 根据spuID查询sku的集合
     * @param spuId
     * @return sku集合
     */
    @GetMapping("/goods/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> findSkuBySpuId(@RequestParam("id") Long spuId) {
        return ResponseEntity.ok(skuService.findSkuBySpuId(spuId));
    }

    /**
     * 根据id查询商品详情
     * @param spuId 商品id
     * @return 商品详情信息
     */
    @GetMapping("/goods/spu/detail")
    public ResponseEntity<SpuDetailDTO> findSpuDetailBySpuId(@RequestParam("id") Long spuId) {
        return ResponseEntity.ok(spuDetailService.findSpuDetailBySpuId(spuId));
    }

    /**
     * 根据id查询spu及sku、spuDetail等
     * @param spuId
     * @return
     */
    @GetMapping("/goods/{id}")
    public ResponseEntity<SpuDTO> findSpuAllBySpuId(@PathVariable("id") Long spuId) {
        return ResponseEntity.ok(spuService.findSpuAllBySpuId(spuId));
    }

    /**
     * 根据id查询商品
     * @param spuId
     * @return
     */
    @GetMapping("/goods/spu/{id}")
    public ResponseEntity<SpuDTO> findSpuBySpuId(@PathVariable("id") Long spuId) {
        return ResponseEntity.ok(spuService.findSpuBySpuId(spuId));
    }

    /**
     * 新增商品
     * @param spuDTO
     * @return
     */
    @PostMapping("/goods/spu")
    public ResponseEntity<Void> addSpu(@RequestBody SpuDTO spuDTO) {
        spuService.addSpu(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新商品
     * @param spuDTO
     * @return
     */
    @PutMapping("/goods/spu")
    public ResponseEntity<Void> updateSpu(@RequestBody SpuDTO spuDTO) {
        spuService.updateSpu(spuDTO);
        return ResponseEntity.status(200).build();
    }

    /**
     * 修改商品上下架
     * @param spuId
     * @param saleable
     * @return
     */
    @PutMapping("/goods/saleable")
    public ResponseEntity<Void> updateSpuSaleable(
            @RequestParam(value = "id") Long spuId,
            @RequestParam(value = "saleable") Boolean saleable
    ) {
        spuService.updateSpuSaleable(spuId, saleable);
        return ResponseEntity.status(200).build();
    }


    /**
     * 批量减库存
     * @param map
     * @return
     */
    @PutMapping("/goods/stock/minus")
    public ResponseEntity<Void> updateSkuStock(
            @RequestBody Map<Long, Long> map
    ) {
        skuService.updateSkuStock(map);
        return ResponseEntity.status(200).build();
    }






}
